package com.insurance.exception;


import com.insurance.common.ResultData;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created with IntelliJ IDEA.
 * User: zdk
 * Date: 2020/10/20 11:13
 * Description:
 * Version: V1.0
 */
//@ControllerAdvice
public class ExceptionController {

    @ExceptionHandler(NumberFormatException.class)
    @ResponseBody
    public ResultData resultNumberFormatException(){ //统一返回异常信息
        ResultData responseData = new ResultData();
        responseData.setCode(500);
        responseData.setMsg("数字转换异常");
        responseData.setData(null);
        return responseData;
    }
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public ResultData resultArithmeticException(){ //统一返回异常信息
        ResultData responseData = new ResultData();
        responseData.setCode(500);
        responseData.setMsg("计算除零异常");
        responseData.setData(null);
        return responseData;
    }


    @ExceptionHandler(UnknownAccountException.class)
    @ResponseBody
    public ResultData resultUnknownAccountException(){ //统一返回异常信息
        ResultData responseData = new ResultData();
        responseData.setCode(500);
        responseData.setMsg("员工用名称或密码有误");
        responseData.setData(null);
        return responseData;
    }

    @ExceptionHandler(IncorrectCredentialsException.class)
    @ResponseBody
    public ResultData resultIncorrectCredentialsException(){ //统一返回异常信息
        ResultData responseData = new ResultData();
        responseData.setCode(500);
        responseData.setMsg("员工用名称或密码有误");
        responseData.setData(null);
        return responseData;
    }

    @ExceptionHandler(UnauthorizedException.class)
    @ResponseBody
    public ResultData resultUnauthorizedException(){ //统一返回异常信息
        ResultData responseData = new ResultData();
        responseData.setCode(500);
        responseData.setMsg("没有权限");
        responseData.setData(null);
        return responseData;
    }

    @ExceptionHandler(LockedAccountException.class)
    @ResponseBody
    public ResultData resultLockedAccountException(){ //统一返回异常信息
        ResultData responseData = new ResultData();
        responseData.setCode(500);
        responseData.setMsg("账号被锁");
        responseData.setData(null);
        return responseData;
    }

    @ExceptionHandler(AuthenticationException.class)
    @ResponseBody
    public ResultData resultAuthenticationException(){ //统一返回异常信息
        ResultData responseData = new ResultData();
        responseData.setCode(500);
        responseData.setMsg("认证错误");
        responseData.setData(null);
        return responseData;
    }


//    @ExceptionHandler(Throwable.class)
//    @ResponseBody
//    public ResultData resultOtherException(){ //统一返回异常信息
//        ResultData responseData = new ResultData();
//        responseData.setCode(500);
//        responseData.setMsg("系统开小差");
//        responseData.setData(null);
//        return responseData;
//    }

}
